package com.freesky.biz.abchina.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesky.biz.abchina.domain.LifepayBillEntity;
import com.freesky.biz.abchina.domain.LifepayOrderEntity;
import com.freesky.biz.abchina.mapper.LifepayBillMapper;
import com.freesky.biz.abchina.mapper.LifepayOrderMapper;
import com.freesky.biz.abchina.service.ILifepayBillService;
import com.freesky.biz.abchina.service.ILifepayOrderService;
import com.freesky.biz.rights.domain.RightsPayEntity;
import com.freesky.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class LifepayOrderServiceImpl extends ServiceImpl<LifepayOrderMapper, LifepayOrderEntity> implements ILifepayOrderService {

    @Override
    public List<LifepayOrderEntity> selectUnRechargeList() {
        LambdaQueryWrapper<LifepayOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifepayOrderEntity::getProcessed,"0");
        queryWrapper.eq(LifepayOrderEntity::getStatus,"1");
        queryWrapper.eq(LifepayOrderEntity::getNeedRefund,"0");
        queryWrapper.eq(LifepayOrderEntity::getRefundFlag,"0");
        queryWrapper.eq(LifepayOrderEntity::getDelFlag,"0");
        queryWrapper.orderByDesc(LifepayOrderEntity::getCreateTime).last("limit 500");
        return list(queryWrapper);
    }

    @Override
    public List<LifepayOrderEntity> selectRefundOrderList() {
        LambdaQueryWrapper<LifepayOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifepayOrderEntity::getProcessed,"3");
        queryWrapper.eq(LifepayOrderEntity::getStatus,"1");
        queryWrapper.eq(LifepayOrderEntity::getNeedRefund,"1");
        queryWrapper.eq(LifepayOrderEntity::getRefundFlag,"0");
        queryWrapper.eq(LifepayOrderEntity::getDelFlag,"0");
        queryWrapper.orderByDesc(LifepayOrderEntity::getCreateTime);
        return list(queryWrapper);
    }


    @Override
    public List<Map<String, Object>> selectLifepayOrderMaps(String startDt, String endDt) {

//        LambdaQueryWrapper<LifepayOrderEntity> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(LifepayOrderEntity::getStatus, 1).ge(LifepayOrderEntity::getCreateTime,startDt);
//        wrapper.select("SUM(pay_bill_amt) AS sumPayBillAmt","processed","COUNT(*) AS count");
//        wrapper.groupBy(LifepayOrderEntity::getProcessed);
//        return listMaps(wrapper);

        QueryWrapper<LifepayOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1).between("create_time", startDt, endDt)
                .select("SUM(pay_bill_amt) AS sumPayBillAmt",
                        "processed",
                        "COUNT(*) AS count")
                .groupBy("processed");
        return listMaps(wrapper);
    }
}
